import 'package:flutter/widgets.dart';
import 'package:netease_cloud_music_api/netease_cloud_music_api.dart';
import 'package:netease_cloud_music_api/utils/enum.dart';
import 'package:netease_cloud_music_api/utils/player_request.dart';
import 'package:netease_cloud_music_api/utils/request.dart';

mixin Search {
  /// 搜索
  /// 说明 : 调用此接口 , 传入搜索关键词可以搜索该音乐 / 专辑 / 歌手 / 歌单 / 用户 , 关键词可以多个 , 以空格隔开 , 如 " 周杰伦 搁浅 "( 不需要登录 ),
  /// 搜索获取的 mp3url 不能直接用
  /// [keywords] 关键词
  /// [searchType] 搜索类型 [SearchType.Songs] 为单曲, [SearchType.Album] 为专辑, [SearchType.Artist] 为歌手,
  /// [SearchType.Playlist] 为歌单, [SearchType.User] 为用户, [SearchType.MV] 为MV, [SearchType.Lyric] 为歌词
  /// [SearchType.Raido] 为电台, [SearchType.Video] 为视频
  /// [limit] 返回数量 , 默认为 30
  /// [offset] 偏移数量，用于分页 ,如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认为 0
  Future<PlayerRequestResponse> search({required keywords, SearchType searchType = SearchType.Songs, int? limit, int? offset}) {
    Map<SearchType, int> type = {
      SearchType.Songs: 1,
      SearchType.Album: 10,
      SearchType.Artist: 100,
      SearchType.Playlist: 1000,
      SearchType.User: 1002,
      SearchType.MV: 1004,
      SearchType.Lyric: 1006,
      SearchType.Raido: 1009,
      SearchType.Video: 1014
    };
    Map<String, dynamic> data = {'s': keywords, 'type': type[searchType], 'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true};
    print(data);

    return PlayerRequest(
      url: 'https://music.163.com/api/cloudsearch/pc',
      data: data,
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 默认搜索关键词
  /// 说明 : 调用此接口 , 可获取默认搜索关键词
  Future<PlayerRequestResponse> searchDefault() {
    return PlayerRequest(
      url: 'https://interface3.music.163.com/eapi/search/defaultkeyword/get',
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 热搜列表(详细)
  /// 说明 : 调用此接口,可获取热门搜索列表
  Future<PlayerRequestResponse> searchHotDetail() {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/hotsearchlist/get',
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 热搜列表(简略)
  /// 说明 : 调用此接口,可获取热门搜索列表
  Future<PlayerRequestResponse> searchHot() {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/search/hot',
      data: { 'type': 1111 },
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post, ua: UserAgent.mobile),
    ).request();
  }

  /// 搜索多重匹配
  /// [keywords] 关键字
  /// 说明 : 调用此接口 , 传入搜索关键词可获得搜索结果
  Future<PlayerRequestResponse> searchMultimatch({ required String keywords }) {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/search/suggest/multimatch',
      data: { 's': keywords},
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }

  /// 搜索建议
  /// 说明 : 调用此接口 , 传入搜索关键词可获得搜索建议 , 搜索结果同时包含单曲 , 歌手 , 歌单 ,mv 信息
  /// [keywords] 关键字
  /// [suggestType] 如果传 [SearchSuggestType.Mobile] 则返回移动端数据
  Future<PlayerRequestResponse> searchSuggest({ required String keywords, SearchSuggestType suggestType = SearchSuggestType.Web }) {
    Map<SearchSuggestType, String> type = {
      SearchSuggestType.Mobile: 'keyword',
      SearchSuggestType.Web: 'web'
    };
    return PlayerRequest(
      url: 'https://music.163.com/weapi/search/suggest/${type[suggestType]}',
      data: { 's': keywords },
      options: PlayerRequestOptions(crypto: EncryptType.weapi, method: PlayerRequestMethod.post),
    ).request();
  }
}
